package com.awifi.com.thread.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @description: ToDo
 * @author: marin
 * @time: 2020/6/28 22:03
 */
public class ThreadLock implements Runnable {
    private int ticket = 100;
    Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock.lock();
            if (ticket > 0) {
                ticket--;
                System.out.println(Thread.currentThread().getName()+"正在售卖第"+ticket+"张");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            lock.unlock();
        }
    }
}
